/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 19182
 * Date: 2025-08-25
 * Time: 20:30
 */
public class demo8 {
    private static int flag = 0;
    private static Object locker = new Object();
    public static void main(String[] args) {
        Thread t1 = new Thread(()->{
            for (int i = 0; i <10; i++) {
                synchronized (locker){
                    while(flag != 0){
                        try {
                            locker.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    System.out.print("A");
                    flag = 1;
                    locker.notifyAll();
                }
            }

        });
        Thread t2 = new Thread(()->{
            for (int i = 0; i < 10; i++) {
                synchronized(locker){
                    while(flag != 1){
                        try {
                            locker.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    System.out.print("B");
                    flag = 2;
                    locker.notifyAll();
                }
            }
        });
        Thread t3 = new Thread(()->{
            for(int i = 0; i < 10; i++){
                synchronized (locker){
                    while(flag != 2){
                        try {
                            locker.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    System.out.println("C");
                    flag = 0;
                    locker.notifyAll();
                }
            }

        });
        t1.start();
        t2.start();
        t3.start();
    }
}
